﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using Table_Dal;
namespace ApplicationLayer.ThiSinh
{
    public partial class frmThiSinh : DevComponents.DotNetBar.OfficeForm
    {
        public frmThiSinh()
        {
            InitializeComponent();
        }
        MonHocBLL mhbll;
        private void frmThiSinh_Load(object sender, EventArgs e)
        {
            Loaddgv();
        }

        private void Loaddgv()
        {
            ThiSinhBLL tsbll = new ThiSinhBLL();
            List<ThiSinhTbl> dsts = tsbll.LayTatCaThiSinh();
            dgvThiSinh.DataSource = dsts;
            dgvThiSinh.Columns["TENSV"].HeaderText = "Mã SV";
            dgvThiSinh.Columns["TENSV"].HeaderText = "Họ Tên";
            dgvThiSinh.Columns["TENSV"].Width = 250;
            dgvThiSinh.Columns["MATKHAU"].HeaderText = "Mật khẩu";
            
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThiSinhTbl ts = null;
            ThiSinh.EditThiSinh frm = new EditThiSinh(ts);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Loaddgv();
                MessageBox.Show("Thêm thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        public bool KiemTra(string k)
        {
            foreach (string item in lstMssv)
            {
                if (item.Equals(k) == true)
                    return true;
            }
            return false;
        }
        List<string> lstMssv = new List<string>();
        private void btnSua_Click(object sender, EventArgs e)
        {
            lstMssv.Clear();
            for (int i = 0; i < dgvThiSinh.SelectedCells.Count; i++)
            {
                if (KiemTra(dgvThiSinh.Rows[dgvThiSinh.SelectedCells[i].RowIndex].Cells[0].Value.ToString()) == false)
                    lstMssv.Add(dgvThiSinh.Rows[dgvThiSinh.SelectedCells[i].RowIndex].Cells[0].Value.ToString());
            }
            if (lstMssv.Count == 0)
            {
                MessageBox.Show("Bạn chưa chọn Thí Sinh nào", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (lstMssv.Count > 1)
            {
                MessageBox.Show("Bạn chỉ được chỉnh sửa trên 1 Thí Sinh","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            ThiSinhBLL tsbll = new ThiSinhBLL();
            ThiSinhTbl ts = new ThiSinhTbl();
            ts = tsbll.ThongTinThiSinh(lstMssv[0]);
            ThiSinh.EditThiSinh frm = new EditThiSinh(ts);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Loaddgv();
                MessageBox.Show("Sửa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void dgvThiSinh_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            
            if (MessageBox.Show("Bạn muốn xóa thí sinh?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                for (int i = 0; i < dgvThiSinh.SelectedCells.Count; i++)
                {
                    if (KiemTra(dgvThiSinh.Rows[dgvThiSinh.SelectedCells[i].RowIndex].Cells[0].Value.ToString()) == false)
                        lstMssv.Add(dgvThiSinh.Rows[dgvThiSinh.SelectedCells[i].RowIndex].Cells[0].Value.ToString());
                }
                ThiSinhBLL tsbll = new ThiSinhBLL();
                foreach (string item in lstMssv)
                {
                    tsbll.Xoa(item);
                }
                Loaddgv();
                MessageBox.Show("Xóa thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        
    }
}